package cn.ekuma.epos.datalogic.define.dao;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.serialize.DataRead;
import com.openbravo.data.loader.serialize.DataWrite;
import com.openbravo.data.loader.serialize.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.table.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

import e.odbo.data.dao.BaseDAO;
import cn.ekuma.epos.symmetricds.AppConfig;
import cn.ekuma.epos.symmetricds.bean.Node;

public class NodeDAO extends BaseDAO<Node> {

	public NodeDAO(I_Session s) {
		super(s);
	}

	@Override
	public Node readValues(DataRead dr,Node obj) throws BasicException {
		if(obj==null)
			obj=new Node();
		obj.setNodeId(dr.getString(1));
		obj.setNodeGroupId(dr.getString(2));
		obj.setExternalId(dr.getString(3));
		obj.setSyncEnabled(dr.getBoolean(4));
		obj.setSyncUrl(dr.getString(5));
		
		obj.setSchemaVersion(dr.getString(6));
		obj.setSymmetricVersion(dr.getString(7));
		obj.setDatabaseType(dr.getString(8));
		obj.setDatabaseVersion(dr.getString(9));
		obj.setHeartbeatTime(dr.getTimestamp(10));
		
		obj.setTimezoneOffset(dr.getString(11));
		obj.setBatchToSendCount(dr.getInt(12));
		obj.setBatchInErrorCount(dr.getInt(13));
		obj.setCreatedAtNodeId(dr.getString(14));
		obj.setDeploymentType(dr.getString(15));
		return obj;
	}

	@Override
	public TableDefinition getTable() {
		return new TableDefinition(
				AppConfig.getAppProperty(AppConfig.TABLE_PREFIX)+"_"+"node",
                new Field[]{
                   new Field("node_id",Datas.STRING, Formats.STRING),
                   new Field("node_group_id",Datas.STRING, Formats.STRING),
                   new Field("external_id",Datas.STRING, Formats.STRING),
                   new Field("sync_enabled",Datas.BOOLEAN, Formats.BOOLEAN),
                   new Field("sync_url",Datas.STRING, Formats.STRING),
                   
                   new Field("schema_version",Datas.STRING, Formats.STRING),
                   new Field("symmetric_version",Datas.STRING, Formats.STRING),
                   new Field("database_type",Datas.STRING, Formats.STRING),
                   new Field("database_version",Datas.STRING, Formats.STRING),
                   new Field("heartbeat_time",Datas.TIMESTAMP, Formats.TIMESTAMP,true),
                   
                   new Field("timezone_offset",Datas.STRING, Formats.STRING,true),
                   new Field("batch_to_send_count",Datas.INT, Formats.INT),
                   new Field("batch_in_error_count",Datas.INT, Formats.INT),
                   new Field("created_at_node_id",Datas.STRING, Formats.STRING),
                   new Field("deployment_type",Datas.STRING, Formats.STRING)
                 },
                new int[]{0}
                );
	}

	/**
    <table name="node" description="Representation of an instance of SymmetricDS that synchronizes data with one or more additional nodes. Each node has a unique identifier (nodeId) that is used when communicating, as well as a domain-specific identifier (externalId) that provides context within the local system.">
        <column name="node_id" type="VARCHAR" size="50" required="true" primaryKey="true" description="A unique identifier for a node." />
        <column name="node_group_id" type="VARCHAR" size="50" required="true" description="The node group that this node belongs to, such as 'store'." />
        <column name="external_id" type="VARCHAR" size="50" required="true" description="A domain-specific identifier for context within the local system. For example, the retail store number. "/>
        <column name="sync_enabled" type="BOOLEANINT" size="1" default="0" description="Indicates whether this node should be sent synchronization. Disabled nodes are ignored by the triggers, so no entries are made in data_event for the node."/>
        <column name="sync_url" type="VARCHAR" size="255" description="The URL to contact the node for synchronization." />
        
        <column name="schema_version" type="VARCHAR" size="50" description="The version of the database schema this node manages. Useful for specifying synchronization by version. "/>
        <column name="symmetric_version" type="VARCHAR" size="50"  description="The version of SymmetricDS running at this node." />
        <column name="database_type" type="VARCHAR" size="50"  description="The database product name at this node as reported by JDBC."  />
        <column name="database_version" type="VARCHAR" size="50"  description="The database product version at this node as reported by JDBC." />
        <column name="heartbeat_time" type="TIMESTAMP"  description="Deprecated.  Use node_host.heartbeat_time instead." />
        
        <column name="timezone_offset" type="VARCHAR" size="6"  description="Deprecated.  Use node_host.timezone_offset instead." />
        <column name="batch_to_send_count" type="INTEGER" default="0" description="The number of outgoing batches that have not yet been sent.  This field is updated as part of the heartbeat job if the heartbeat.update.node.with.batch.status property is set to true." />
        <column name="batch_in_error_count" type="INTEGER" default="0" description="The number of outgoing batches that are in error at this node.  This field is updated as part of the heartbeat job if the heartbeat.update.node.with.batch.status property is set to true." />
        <column name="created_at_node_id" type="VARCHAR" size="50"  description="The node_id of the node where this node was created. This is typically filled automatically with the node_id found in node_identity where registration was opened for the node. " />
        <column name="deployment_type" type="VARCHAR" size="50"  description="An indicator as to the type of SymmetricDS software that is running.  Possible values are, but not limited to: engine, standalone, war, professional, mobile" />
    </table>
	 */
	
	
	@Override
	public void writeInsertValues(DataWrite dp, Node obj)
			throws BasicException {
		dp.setString(1,obj.getNodeId());
		dp.setString(2,obj.getNodeGroupId());
		dp.setString(3,obj.getExternalId());
		dp.setBoolean(4,obj.isSyncEnabled());
		dp.setString(5,obj.getSyncUrl());
		
		dp.setString(6,obj.getSchemaVersion());
		dp.setString(7,obj.getSymmetricVersion());
		dp.setString(8,obj.getDatabaseType());
		dp.setString(9,obj.getDatabaseVersion());
		dp.setTimestamp(10,obj.getHeartbeatTime());
		
		dp.setString(11,obj.getTimezoneOffset());
		dp.setInt(12,obj.getBatchToSendCount());
		dp.setInt(13,obj.getBatchInErrorCount());
		dp.setString(14,obj.getCreatedAtNodeId());
		dp.setString(15,obj.getDeploymentType());

	}

	@Override
	public Class getSuportClass() {
		return Node.class;
	}

}
